<?php

namespace plugin\admin\app\controller;

use support\Request;
use support\Response;
use plugin\admin\app\model\Withdraw;
use plugin\admin\app\controller\Crud;
use support\exception\BusinessException;

/**
 * 提现列表 
 */
class WithdrawController extends Crud
{
    
    /**
     * @var Withdraw
     */
    protected $model = null;

    /**
     * 构造函数
     * @return void
     */
    public function __construct()
    {
        $this->model = new Withdraw;
    }

    public function agree(Request $request)
    {
        $id = $request->post('id');
        $withdraw = $this->model->find($id);
        if ($withdraw->status == 1){
            return json(['code'=>1,'msg'=>'已打款']);
        }
        $withdraw->status = 1;
        $withdraw->save();
        return json(['code'=>0,'msg'=>'打款成功']);

    }

    public function refuse(Request $request)
    {
        $id = $request->post('id');
        $withdraw = $this->model->find($id);
        if ($withdraw->status == 2){
            return json(['code'=>1,'msg'=>'已拒绝']);
        }
        $withdraw->status = 2;
        $withdraw->save();
        return json(['code'=>0,'msg'=>'拒绝成功']);
    }
    
    /**
     * 浏览
     * @return Response
     */
    public function index(): Response
    {
        return view('withdraw/index');
    }

    /**
     * 插入
     * @param Request $request
     * @return Response
     * @throws BusinessException
     */
    public function insert(Request $request): Response
    {
        if ($request->method() === 'POST') {
            return parent::insert($request);
        }
        return view('withdraw/insert');
    }

    /**
     * 更新
     * @param Request $request
     * @return Response
     * @throws BusinessException
    */
    public function update(Request $request): Response
    {
        if ($request->method() === 'POST') {
            return parent::update($request);
        }
        return view('withdraw/update');
    }

}
